/* Copyright (c) 2008, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#include "mboot.h"
#include "stdint.h"
#include "kif.h"
#include "malloc.h"
#include "memmgr.h"
#include "vmem.h"
#include "console.h"

struct Multiboot_Header *kif;
extern struct linked_mmap *first;
extern char tysos_heap_start, tysos_heap_end, bda_addr;

void *make_kif()
{
	int mmap_items, i;
	void *mmap_array_obj;
	struct linked_mmap *cur_linked_mmap;

	kif = (struct Multiboot_Header *)malloc(sizeof(struct Multiboot_Header));

	/* Build the managed array of MemoryMap objects */
	mmap_items = count_linked_list();
	Create_Ref_Array(&mmap_array_obj, mmap_items);

	cur_linked_mmap = first;
	for(i = 0; i < mmap_items; i++) {
		*Get_Ref_Elem(mmap_array_obj, i) = (unsigned long long int)(unsigned long int)&cur_linked_mmap->mmap;
		cur_linked_mmap = cur_linked_mmap->next;
	}

	/* Link it into the kif */
	kif->mmap = (unsigned long long int)(unsigned long int)mmap_array_obj;

	/* The loader needs to provide a space which the kernel can use as its default heap, because it is already running
	in paged mode and has little knowledge of the layout of the virtual address space */
	kif->heap_start = (unsigned long long int)(unsigned long int)&tysos_heap_start;
	kif->heap_end = (unsigned long long int)(unsigned long int)&tysos_heap_end;

	/* We also need to provide the virtual address of the level 4 page table */
	kif->virt_master_paging_struct = VA_PML4T;

	/* Inform tysos where we've mapped the bios data area */
	kif->virt_bda = (unsigned long long int)(unsigned long int)&bda_addr;

#ifdef _DEBUG
	_puts("kif: ", 0);
	puthex((unsigned long int)kif);
	_puts("  mmap_array:", 0);
	puthex((unsigned long int)mmap_array_obj);
	_puts("", 1);
#endif

	return kif;
}
